/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.backingBeans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.Size;
import net.tutwiz.model.Step;
import net.tutwiz.model.Tutorial;
import net.tutwiz.persistence.StepPersistenceManager;
import net.tutwiz.persistence.TutorialPersistenceManager;

/**
 * Backs up the view of editStep.xhtml
 *
 * @author vind
 */
@ManagedBean(name = "editStepBB")
@ViewScoped
public class EditStepBackingBean {

    //Only used for validation
    @Size(min = 5, message = "The title should be clear and descriptive, please use at least 5 characters.")
    String title;
    @Size(min = 10, message = "Are you sure you covered that step properly?")
    String text;
    
    Step step;
    Tutorial tutorial;
    TutorialPersistenceManager tpm;

    public EditStepBackingBean() {
        tpm = new TutorialPersistenceManager();
        Long tutorialId = null;
        Long stepId = null;

        try {
            Map<String, String> parameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
            tutorialId = Long.valueOf(parameters.get("tutorial"));
            stepId = Long.valueOf(parameters.get("step"));
        } catch (Exception ex) {
            //missing step parameter, no worries.
        }

        tutorial = tpm.get(tutorialId);

        if (stepId == null) {
            step = new Step(tutorial, tutorial.getSteps().size(), "The Step Title", "The Step Text");
        } else {
            //Find the step with id from get parameter
            List<Step> steps = tutorial.getSteps();
            for (int i = 0; i < steps.size(); i++) {
                if (steps.get(i).getId().equals(stepId)) {
                    step = steps.get(i);
                }
            }
        }
    }

    public String getText() {
        return step.getText();
    }

    public void setText(String text) {
        this.step.setText(text);
    }

    public String getTitle() {
        return step.getTitle();
    }

    public void setTitle(String title) {
        this.step.setTitle(title);
    }
    
    public String getTutorialID() {
        return "" + tutorial.getId();
    }

    public void saveStep() {
        List<Step> steps = tutorial.getSteps();
        
        if (step.getId() == null) {
            steps.add(step);
        } else {
            
        }
        
        tpm.update(tutorial);
    }
    
    /*
     * Not correctly implemented yet, tries to update the step order after
     * step removal.
     */
    public void deleteStep(Long id) {
        List<Step> oldList = tutorial.getSteps();
        List<Step> newList = new ArrayList<Step>();
        
        boolean found = false;
        for(int i = 0; i < oldList.size(); i++) {
            Step currentStep = oldList.get(i);
            
            if(found) {
                currentStep.setStepOrder(currentStep.getStepOrder()-1);
            }
            
            if(currentStep.getId() == step.getId()) {
                found = true;
            } else {
                newList.add(currentStep);
            }
        }
        
        tutorial.setSteps(newList);
        tpm.save(tutorial);
    }
}
